VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "EmbededConsole"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long


Private Const WM_Close As Long = &H10
Private Const MF_BYPOSITION As Long = &H400
Private Const MF_REMOVE As Long = &H1000

Private Const SM_CXBORDER As Long = 5
Private Const SM_CYBORDER As Long = 6
Private Const SM_CYCAPTION As Long = 4
'Private Const SM_CYVSCROLL As Long = 20
'Private Const SM_CXHSCROLL As Long = 21
'Private Const SM_CXVSCROLL As Long = 2
Private Const SM_CYHSCROLL As Long = 3
Private Const SM_CYFRAME As Long = 33
Private Const SM_CXFRAME As Long = 32

Private Const SWP_SHOWWINDOW As Long = &H40
Private Const SWP_NOACTIVE As Long = &H10
Private Const SWP_FRAMECHANGED As Long = &H20
'Private Const CONSOLE_CLASS_STRING As String = "ConsoleWindowClass"

Private m_hConsole As Long
Private m_lProcessID As Long
Private m_ConsoleExe As String
Dim WithEvents ConsoleBox As PictureBox
Attribute ConsoleBox.VB_VarHelpID = -1


Public Sub bHost(ByRef tHost As PictureBox)
  Set ConsoleBox = tHost
End Sub



Public Sub Revive()
    m_hConsole = CreateConsole(m_lProcessID)
    If m_hConsole = 0 Then Exit Sub
    SetParent m_hConsole, ConsoleBox.hwnd
    
    Dim lBorderWidthX As Long, lBorderWidthY As Long, lCaptionHeight As Long
    Dim lScrollWidth As Long ',lScrollWidthX As Long
    
    lBorderWidthX = GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CXBORDER)
    lBorderWidthY = GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYBORDER)
    lCaptionHeight = GetSystemMetrics(SM_CYCAPTION)
    lScrollWidth = GetSystemMetrics(SM_CYHSCROLL)
    Dim fontFix As Integer, X As Integer, Y As Integer
    X = ConsoleBox.ScaleWidth
    Y = ConsoleBox.ScaleHeight
    SetWindowPos m_hConsole, 0, 0 - lBorderWidthX, 0 - lBorderWidthY - lCaptionHeight, X + lBorderWidthX * 2, Y + lCaptionHeight + 2 * lBorderWidthY, _
                SWP_SHOWWINDOW + SWP_NOACTIVE + SWP_FRAMECHANGED
    Call KillSysMenu(m_hConsole)
    
'a quick but dirty way to get minimized on start
    DoEvents
    If TorCtrl.MiniRunOnStart.Value = 1 Then
        TorCtrl.isInTray = True
        TorCtrl.Hide
    End If
End Sub

Public Function Die() As Long
    Call SendMessage(m_hConsole, WM_Close, 0, 0)
    m_lProcessID = vbNull
    m_hConsole = vbNull
End Function

Public Function Close_Console_By_File_Path(ByRef sTitle As String) As Boolean
Dim hConsole As Long
Do
hConsole = FindWindow("ConsoleWindowClass", sTitle)
DoEvents
If hConsole <> 0 Then Call SendMessage(hConsole, WM_Close, 0, 0)
Loop Until hConsole = 0
End Function

Public Property Get hEmbdWnd() As Long
    hEmbdWnd = m_hConsole
End Property


Public Property Let sc32_Path(ByVal var1 As String)
    m_ConsoleExe = var1
End Property

Private Sub KillSysMenu(ByVal hTargetWnd As Long)

    Dim hMenu As Long, i As Long

    hMenu = GetSystemMenu(hTargetWnd, 0)
  
    If hMenu <> 0 Then
        For i = 1 To 7
            Call RemoveMenu(hMenu, 0, MF_REMOVE Or MF_BYPOSITION)
        Next
    End If
End Sub




Private Function CreateConsole(ByRef lProcessID As Long) As Long
    Dim hConsole As Long
    Dim lPID As Long
    If m_ConsoleExe = "" Then Exit Function
    
    
    lProcessID = Shell(m_ConsoleExe, vbHide)
    
    
    Do
        hConsole = FindWindow("#32770", "")
        If hConsole <> 0 Then SendMessage hConsole, WM_Close, 0, 0 ': Exit Do
        DoEvents
    Loop While hConsole = 0
    

    Do
        hConsole = FindWindow(vbNullString, "SocksCap Control")
        DoEvents
    Loop While hConsole = 0
    
    CreateConsole = hConsole
End Function



Private Sub Class_Terminate()
    Call Die
End Sub


